﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Touch2Like.SharedLibrary;
using Touch2Like.Model;

namespace Touch2Like.DAL
{
    public class UserDAL
    {
        public int UpdateUser(UserInfo info, string actionPerformed)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserGuid",SqlDbType.UniqueIdentifier, 16, new Guid(info.UserGuid)),
                db.MakeInParam("@StripeCustomerID",SqlDbType.VarChar, 50, info.StripeCustomerID),
                db.MakeInParam("@FirstName", SqlDbType.VarChar, 50, info.FirstName),
                db.MakeInParam("@LastName", SqlDbType.VarChar, 50, info.LastName),
                db.MakeInParam("@BusinessName", SqlDbType.VarChar, 50, info.BusinessName),
                db.MakeInParam("@AddressLine1", SqlDbType.VarChar, 50, info.AddressLine1),
                db.MakeInParam("@AddressLine2", SqlDbType.VarChar, 50, info.AddressLine2),
                db.MakeInParam("@City", SqlDbType.VarChar, 50, info.City),
                db.MakeInParam("@State", SqlDbType.VarChar, 10, info.State),
                db.MakeInParam("@Zipcode", SqlDbType.VarChar, 10, info.Zipcode), 
                db.MakeInParam("@UserName",SqlDbType.VarChar,50, info.UserName),
                db.MakeInParam("@Password",SqlDbType.VarChar,50,info.Password),
                db.MakeInParam("@Email",SqlDbType.VarChar,50,info.Email),
                db.MakeInParam("@Phone",SqlDbType.VarChar,100, info.Phone),
                db.MakeInParam("@Longitude", SqlDbType.VarChar, 50, info.Longitude), 
                db.MakeInParam("@Latitude", SqlDbType.VarChar, 50, info.Latitude),
                db.MakeInParam("@RegistrationCode",SqlDbType.Int, 8, info.RegistrationCode),
                db.MakeInParam("@ActionPerformed", SqlDbType.VarChar, 50, actionPerformed)
            };
            try
            {
                db.ExecNonQuery("CAM_User_Update", param);
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        /// <summary>
        /// Get User information by name or Email
        /// </summary>
        /// <param name="type">0 = Username; 1 = Email</param>
        /// <param name="content">Content</param>
        /// <returns></returns>
        public UserInfo GetUserByNameAndEmail(int type, string content)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@Type",SqlDbType.Int,4,type),
                db.MakeInParam("@Content",SqlDbType.VarChar,200,content)
            };
            try
            {
                dr = db.RunProc("CAM_User_GetByUserNameAndEmail", param);
                if (dr.Read())
                {
                    UserInfo info = new UserInfo();
                    info.AccountType = DataHelper.GetInt(dr["AccountType"]);
                    info.Active = DataHelper.GetInt32(dr["Active"]);
                    info.AddressLine1 = DataHelper.GetString(dr["AddressLine1"]);
                    info.AddressLine2 = DataHelper.GetString(dr["AddressLine2"]);
                    info.BusinessName = DataHelper.GetString(dr["BusinessName"]);
                    info.City = DataHelper.GetString(dr["City"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.ExpireDate = DataHelper.GetDateTime(dr["ExpireDate"]);
                    info.FirstName = DataHelper.GetString(dr["FirstName"]);
                    info.LastName = DataHelper.GetString(dr["LastName"]);
                    info.Latitude = DataHelper.GetString(dr["Latitude"]);
                    info.Longitude = DataHelper.GetString(dr["Longitude"]);
                    info.Password = DataHelper.GetString(dr["Password"]);
                    info.Phone = DataHelper.GetString(dr["Phone"]);
                    info.State = DataHelper.GetString(dr["State"]);
                    info.StripeCustomerID = DataHelper.GetString(dr["StripeCustomerID"]);
                    info.UserGuid = DataHelper.GetString(dr["UserGUID"]);
                    info.UserName = DataHelper.GetString(dr["Username"]);
                    info.Void = DataHelper.GetInt32(dr["Void"]);
                    info.Zipcode = DataHelper.GetString(dr["Zipcode"]);
                    LoginTrack(info.UserName, info.UserGuid, 0);
                    return info;

                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }

        private void LoginTrack(string UserName, string UserGUID, int IsMobileApp)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@UserName",SqlDbType.VarChar,50,UserName),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(UserGUID)),
                db.MakeInParam("@IsMobileApp",SqlDbType.Int,4,IsMobileApp)
            };

            db.ExecNonQuery("CAM_LoginTrack_Update", param);
        }

        public UserInfo GetUserByUserGuid(string userGuid)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@UserGuid",SqlDbType.UniqueIdentifier,32, new Guid(userGuid))
            };
            try
            {
                dr = db.RunProc("CAM_User_GetByUserGUID", param);
                if (dr.Read())
                {
                    UserInfo info = new UserInfo();
                    info.AccountType = DataHelper.GetInt(dr["AccountType"]);
                    info.Active = DataHelper.GetInt32(dr["Active"]);
                    info.AddressLine1 = DataHelper.GetString(dr["AddressLine1"]);
                    info.AddressLine2 = DataHelper.GetString(dr["AddressLine2"]);
                    info.BusinessName = DataHelper.GetString(dr["BusinessName"]);
                    info.City = DataHelper.GetString(dr["City"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.ExpireDate = DataHelper.GetDateTime(dr["ExpireDate"]);
                    info.FirstName = DataHelper.GetString(dr["FirstName"]);
                    info.LastName = DataHelper.GetString(dr["LastName"]);
                    info.Latitude = DataHelper.GetString(dr["Latitude"]);
                    info.Longitude = DataHelper.GetString(dr["Longitude"]);
                    info.Password = DataHelper.GetString(dr["Password"]);
                    info.Phone = DataHelper.GetString(dr["Phone"]);
                    info.State = DataHelper.GetString(dr["State"]);
                    info.StripeCustomerID = DataHelper.GetString(dr["StripeCustomerID"]);
                    info.UserGuid = DataHelper.GetString(dr["UserGUID"]);
                    info.UserName = DataHelper.GetString(dr["Username"]);
                    info.Void = DataHelper.GetInt32(dr["Void"]);
                    info.Zipcode = DataHelper.GetString(dr["Zipcode"]);

                    return info;

                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }

        public void UpdateSetting(UserSettingInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier, 16, new Guid(info.UserGUID)),
                db.MakeInParam("@FlagName",SqlDbType.VarChar, 100, info.FlagName),
                db.MakeInParam("@Value", SqlDbType.VarChar, 50, info.Value)
            };
            try
            {
                db.ExecNonQuery("CAM_UserSetting_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetSettingByUserGUID(string userguid)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier, 16, new Guid(userguid)),
            };
            try
            {
                return db.GetDataSet("CAM_UserSetting_GetByUserGUID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        #region Get All
        public DataTable GetAll()
        {
            Database db = new Database();

            try
            {
                return db.GetDataSet("CAM_User_GetAll").Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Delete
        public void Delete(string username)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@Username",SqlDbType.VarChar,50,username)
            };
            try
            {
                db.ExecNonQuery("CAM_User_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion
    }
}
